﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="finish.aspx.cs" Inherits="OpenRAMS.Maintenance.finish" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Finish Page </title>
    <%
        //This page should never render in the browser.

        //Verify a parameter was sent
        String approveRequest = "";
        try
        {
            approveRequest = Request.QueryString["wtID"].ToString();
        }
        catch { Response.Redirect("Maintenance.aspx"); }

        //verify parameter sent is of integer type
        int wTicketID = -1;
        try
        {
            wTicketID = Convert.ToInt32(approveRequest);
        }
        catch { Response.Redirect("Maintenance.aspx"); }

        //Verify user is in Maintenance role (if they aren't send them back to home)
        if (!User.IsInRole("maintenance") && !User.IsInRole("admin")) { Response.Redirect("~/Staff/Staff.aspx"); }

        //Attempt to find the booking
        WorkTicket wTicketToApprove = new WorkTicket();
        int result = wTicketToApprove.Load(wTicketID);

        //if not found, send back
        if (result < 0) { Response.Redirect("Maintenance.aspx"); }
        
        //do the necessary & save changes
        wTicketToApprove.workTicketActive = false;
        wTicketToApprove.Save();
        
        //Save a work ticket log about this
        WorkTicketLog newLog = new WorkTicketLog();
        UserRecord friendlyName = new UserRecord();
        friendlyName.Load(User.Identity.Name.ToString());
        
        newLog.workTicketLogLinkID = wTicketID;
        newLog.workTicketLogTitle = "Ticket completed.";
        newLog.workTicketLogDesc = "Ticket marked complete by " + friendlyName.userFirstName + " " + friendlyName.userSecondName + ".";
        newLog.workTicketLogCreator = User.Identity.Name.ToString();
        newLog.workTicketLogCategory = "Complete";
        newLog.workTicketLogTimeTakenHours = 0;
        newLog.Save();
        
        Response.Redirect("Maintenance.aspx");
        
    %>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
